//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.dao.wills.PtPackageDao;
import com.aidong.dao.wills.PtPackageItemDao;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.PssProductSale;
import com.aidong.model.wills.PssProductSaleList;
import com.aidong.model.wills.PtContract;
import com.aidong.model.wills.PtPackage;
import com.aidong.model.wills.PtPackageItem;
import com.aidong.web.controller.member.PayController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptPackageSale"})
public class PtPackageSaleController extends PayController {
    @Resource
    PtContractDao ptContractDao;
    @Resource
    PtPackageDao ptPackageDao;
    @Resource
    PtPackageItemDao ptPackageItemDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;

    public PtPackageSaleController() {
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List pkgList = this.ptPackageDao.getShopPackageList(this.getLoginShopCode());
        List coachList = this.ptCoachCodeDao.getShopCoachList(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pt/packagesale/add");
        mv.addObject("pkgList", pkgList);
        mv.addObject("coachList", coachList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("contract") PtContract contract, @RequestParam("payls") String payls) {
        if(this.chkNull(contract.getMemberCode())) {
            return this.getResult(false, "请指定会员");
        } else if(this.chkNull(contract.getPackageCode())) {
            return this.getResult(false, "请选择私教礼包");
        } else if(this.chkNull(contract.getCoachCode())) {
            return this.getResult(false, "请选择上课教练");
        } else {
            PtPackage pkg = (PtPackage)this.ptPackageDao.getById(contract.getPackageCode());
            if(pkg == null) {
                return this.getResult(false, "没有这个礼包");
            } else {
                PtPackageItem pt = null;
                List ptList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "1");
                if(ptList != null && ptList.size() > 0) {
                    pt = (PtPackageItem)ptList.get(0);
                }

                List itemList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "2");
                if(pt != null && itemList != null && itemList.size() != 0) {
                    contract.setPtCode(pt.getItemCode());
                    contract.setQuantity(pt.getQuantity());
                    contract.setFactPrice(pt.getPrice());
                    MemberInfo member = this.memberInfoDao.getByMemberCode(contract.getMemberCode());
                    if(member == null) {
                        return this.getResult(false, "会员信息错误");
                    } else {
                        if(!this.chkNullZ(contract.getEarnestId())) {
                            MemberEarnest saleClassify = (MemberEarnest)this.memberEarnestDao.getById(contract.getEarnestId());
                            if(saleClassify == null || saleClassify.getStatus().intValue() != 0 || !saleClassify.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                return this.getResult(false, "定金不可用");
                            }

                            contract.setEarnestAmount(saleClassify.getEarnestAmount());
                        } else {
                            contract.setEarnestId((Integer)null);
                            contract.setEarnestAmount(Double.valueOf(0.0D));
                        }

                        contract.setShopCode(this.getLoginShopCode());
                        contract.setSaleType("1");
                        contract.setSaleDate(new Date());
                        contract.setBespeakQuantity(Integer.valueOf(0));
                        contract.setClearQuantity(Integer.valueOf(0));
                        contract.setInAmount(Double.valueOf(0.0D));
                        contract.setStatus(Integer.valueOf(1));
                        contract.setCommissionDate(new Date());
                        contract.setReceivePaper(Integer.valueOf(0));
                        contract.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
                        contract.setPrintLabel(Integer.valueOf(0));
                        String saleClassify1 = "2";
                        Integer pn = this.ptContractDao.getCountByMemberCode(contract.getMemberCode());
                        if(!this.chkNullZ(pn)) {
                            saleClassify1 = "1";
                        }

                        Date dt = this.memberContractDao.getFirstDateByMemberCode(contract.getMemberCode());
                        if(this.getAccountDate(new Date()).equals(this.getAccountDate(dt))) {
                            saleClassify1 = "3";
                        }

                        contract.setSaleClassify(saleClassify1);
                        contract.setCreateUser(this.getLoginUserCode());
                        contract.setCreateTime(new Date());
                        contract.setUpdateTime(new Date());
                        contract.setReviewStatus(Integer.valueOf(1));
                        double productPrice = 0.0D;
                        ArrayList productSaleList = new ArrayList();

                        PtPackageItem payAmount;
                        for(Iterator productSale = itemList.iterator(); productSale.hasNext(); productPrice += payAmount.getPrice().doubleValue()) {
                            payAmount = (PtPackageItem)productSale.next();
                            PssProductSaleList saleProdcut = new PssProductSaleList();
                            saleProdcut.setShopCode(this.getLoginShopCode());
                            saleProdcut.setProductCode(payAmount.getItemCode());
                            saleProdcut.setQuantity(Double.valueOf((double)payAmount.getQuantity().intValue()));
                            saleProdcut.setPrice(Double.valueOf(payAmount.getPrice().doubleValue() / (double)payAmount.getQuantity().intValue()));
                            saleProdcut.setCreateUser(this.getLoginUserCode());
                            saleProdcut.setCreateTime(new Date());
                            saleProdcut.setUpdateTime(new Date());
                            productSaleList.add(saleProdcut);
                        }

                        PssProductSale productSale1 = new PssProductSale();
                        productSale1.setShopCode(this.getLoginShopCode());
                        productSale1.setCardCode(member.getCardCode());
                        productSale1.setMemberCode(contract.getMemberCode());
                        productSale1.setSaleDate(new Date());
                        productSale1.setSaleAmount(Double.valueOf(productPrice));
                        productSale1.setEarnestId((Integer)null);
                        productSale1.setEarnestAmount(Double.valueOf(0.0D));
                        productSale1.setCreateUser(this.getLoginUserCode());
                        productSale1.setCreateTime(new Date());
                        productSale1.setUpdateTime(new Date());
                        if(!this.equals(pkg.getTotalPrice().doubleValue(), contract.getFactPrice().doubleValue() + productSale1.getSaleAmount().doubleValue())) {
                            return this.getResult(false, "礼包金额不一致,不能销售");
                        } else {
                            double payAmount1 = pkg.getTotalPrice().doubleValue() - contract.getEarnestAmount().doubleValue();
                            if(contract.getEarnestAmount().doubleValue() - contract.getFactPrice().doubleValue() > 0.001D) {
                                productSale1.setEarnestId(contract.getEarnestId());
                                productSale1.setEarnestAmount(Double.valueOf(contract.getEarnestAmount().doubleValue() - contract.getFactPrice().doubleValue()));
                                contract.setEarnestAmount(contract.getFactPrice());
                            }

                            contract.setPayAmount(Double.valueOf(contract.getFactPrice().doubleValue() - contract.getEarnestAmount().doubleValue()));
                            ArrayList payList = new ArrayList();
                            StringBuilder payMsg = new StringBuilder();
                            PayList pay = this.getPayList(payls, payList, "02", payMsg);
                            if(pay == null) {
                                return this.getResult(false, payMsg.toString());
                            } else if(!this.equals(pay.getAmount().doubleValue(), payAmount1)) {
                                return this.getResult(false, "付款金额不一致");
                            } else {
                                contract.setSalesCode(pay.getSalesCode());
                                contract.setSalesCode2(pay.getSalesCode2());
                                productSale1.setSaleCode(pay.getSalesCode());
                                ArrayList payList1 = new ArrayList();
                                ArrayList payList2 = new ArrayList();
                                double mm = contract.getPayAmount().doubleValue();
                                Iterator e = payList.iterator();

                                double m;
                                PayList pay1;
                                PayList p;
                                while(e.hasNext()) {
                                    pay1 = (PayList)e.next();
                                    if(mm < 0.001D) {
                                        break;
                                    }

                                    if(pay1.getAmount().doubleValue() > 0.001D) {
                                        m = pay1.getAmount().doubleValue() >= mm?mm:pay1.getAmount().doubleValue();
                                        pay1.setAmount(Double.valueOf(pay1.getAmount().doubleValue() - mm));
                                        mm -= m;
                                        p = new PayList();
                                        p.setPayType(pay1.getPayType());
                                        p.setAmount(Double.valueOf(m));
                                        p.setNote(pay1.getNote());
                                        p.setSalesCode(pay1.getSalesCode());
                                        p.setSalesCode2(pay1.getSalesCode2());
                                        p.setCashier(this.getLoginUserCode());
                                        p.setShopCode(this.getLoginShopCode());
                                        p.setSaleType("02");
                                        p.setSaleDate(new Date());
                                        p.setCreateUser(this.getLoginUserCode());
                                        p.setCreateTime(new Date());
                                        p.setUpdateTime(new Date());
                                        payList1.add(p);
                                    }
                                }

                                mm = productSale1.getSaleAmount().doubleValue() - productSale1.getEarnestAmount().doubleValue();
                                e = payList.iterator();

                                while(e.hasNext()) {
                                    pay1 = (PayList)e.next();
                                    if(mm < 0.001D) {
                                        break;
                                    }

                                    if(pay1.getAmount().doubleValue() > 0.001D) {
                                        m = pay1.getAmount().doubleValue() >= mm?mm:pay1.getAmount().doubleValue();
                                        pay1.setAmount(Double.valueOf(pay1.getAmount().doubleValue() - mm));
                                        mm -= m;
                                        p = new PayList();
                                        p.setPayType(pay1.getPayType());
                                        p.setAmount(Double.valueOf(m));
                                        p.setNote(pay1.getNote());
                                        p.setSalesCode(pay1.getSalesCode());
                                        p.setSalesCode2(pay1.getSalesCode2());
                                        p.setCashier(this.getLoginUserCode());
                                        p.setShopCode(this.getLoginShopCode());
                                        p.setSaleType("03");
                                        p.setSaleDate(new Date());
                                        p.setCreateUser(this.getLoginUserCode());
                                        p.setCreateTime(new Date());
                                        p.setUpdateTime(new Date());
                                        payList2.add(p);
                                    }
                                }

                                try {
                                    this.ptPackageDao.addContract(contract, payList1, productSale1, productSaleList, payList2);
                                    this.smsListService.addSmsList("24", contract.getSaleId().intValue());
                                    this.addUserLog("新增私教礼包销售", "合同号:" + contract.getContractCode() + ",会员:" + contract.getMemberCode());
                                } catch (Exception var29) {
                                    this.log.error("add contract", var29);
                                    return this.getResult(false, "保存发生错误");
                                }

                                return this.getSuccess(String.valueOf(contract.getSaleId()), "保存完成");
                            }
                        }
                    }
                } else {
                    return this.getResult(false, "礼包设置不完整,不能购买");
                }
            }
        }
    }

    @RequestMapping({"getPackageItem"})
    @ResponseBody
    public String getPackageItem(@RequestParam("code") String code) {
        PtPackage pkg = (PtPackage)this.ptPackageDao.getById(code);
        if(pkg == null) {
            return this.getResult(false, "没有这个礼包");
        } else {
            PtPackageItem pt = null;
            List ptList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "1");
            if(ptList != null && ptList.size() > 0) {
                pt = (PtPackageItem)ptList.get(0);
            }

            List itemList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "2");
            if(pt != null && itemList != null && itemList.size() != 0) {
                StringBuilder sb = new StringBuilder();
                sb.append("{code:\"").append(pkg.getCode()).append("\"").append(",name:\"").append(pkg.getTotalPrice()).append("\"").append(",totalPrice:").append(pkg.getTotalPrice()).append("").append(",ptCode:\"").append(pt.getItemCode()).append("\"").append(",ptName:\"").append(this.nvl(this.cache.getValue("ptItemName", pt.getItemCode()))).append("\"").append(",ptQuantity:").append(pt.getQuantity()).append("").append(",ptPrice:").append(pt.getPrice()).append("").append(",ptValidDays:").append(pt.getValidDays()).append("").append(",ptCoachLevel:\"").append(this.nvl(pt.getCoachLevel())).append("\"").append(",itemList:[");
                int n = 0;

                PtPackageItem item;
                for(Iterator i$ = itemList.iterator(); i$.hasNext(); sb.append("{pcode:\"").append(item.getItemCode()).append("\"").append(",pname:\"").append(this.nvl(this.cache.getValue("ProductcodeList", item.getItemCode()))).append("\"").append(",pquantity:").append(item.getQuantity()).append("").append(",pprice:").append(item.getPrice()).append("").append("}")) {
                    item = (PtPackageItem)i$.next();
                    if(n++ > 0) {
                        sb.append(",");
                    }
                }

                sb.append("]}");
                return this.getSuccess(sb.toString(), "");
            } else {
                return this.getResult(false, "礼包设置不完整,不能购买");
            }
        }
    }
}
